package com.qixin.api.serviceimpl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qixin.api.bean.*;
import com.qixin.api.mapper.ComCarChargeMapper;
import com.qixin.api.service.ComCarChargeService;
import com.qixin.api.service.ComPtUserService;
import com.qixin.api.service.PtMoneyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 充电车辆信息 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2018-11-16
 */
@Service
public class ComCarChargeServiceImpl extends ServiceImpl<ComCarChargeMapper, ComCarCharge> implements ComCarChargeService {
    @Autowired
    PtMoneyInfoService ptMoneyInfoService;
@Autowired
	ComPtUserService comPtUserService;
    /**
     * 微信公众号 调用  充电结束后 扣除对应金额
     *
     * @param carPlant 车牌
     * @param moeny    扣除的金额
     * @return
     */
    @Override
    public boolean removeMoenyForWeChat(String carPlant, BigDecimal moeny) {
        if (baseMapper.removeMoenyForWeChat(carPlant, moeny) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public ResultPage<ComCarCharge> selectMyPage(VoPage voPage) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<ComCarCharge> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<ComCarCharge> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public boolean giveMoeny(Long id, BigDecimal moeny, HttpSession session) {
        if (baseMapper.giveMoeny(id, moeny) > 0) {
            ComCarCharge comCarCharge = this.selectById(id);
            PtMoneyInfo ptMoneyInfo = new PtMoneyInfo();
            ptMoneyInfo.setFlag(1);
            ptMoneyInfo.setMoeny(moeny);
            ptMoneyInfo.setCreateTime(System.currentTimeMillis());
            ptMoneyInfo.setCarNumber(comCarCharge.getCarBrand());
            ptMoneyInfo.setPartId(id);
            ptMoneyInfo.setPtId(((ComPtUser)session.getAttribute("user")).getId()+" ");
            if(ptMoneyInfoService.insert(ptMoneyInfo)){
               comPtUserService.updatepartgivemoney(ptMoneyInfo);
                return true;
            }

        }
        return false;
    }

    @Override
    public String removeMoeny(Long id, BigDecimal moeny, HttpSession session) {
        ComCarCharge comCarCharge = baseMapper.selectById(id);
        if (comCarCharge.getMoeny().compareTo(moeny) == -1) {
            return "失败，余额不足";
        }

        if (baseMapper.removeMoeny(id, moeny) > 0) {
            PtMoneyInfo ptMoneyInfo = new PtMoneyInfo();
            ptMoneyInfo.setFlag(2);
            ptMoneyInfo.setMoeny(moeny);
            ptMoneyInfo.setCreateTime(System.currentTimeMillis());
            ptMoneyInfo.setCarNumber(comCarCharge.getCarBrand());
            ptMoneyInfo.setPartId(id);
            ptMoneyInfo.setPtId(((ComPtUser)session.getAttribute("user")).getId()+"");
            if(ptMoneyInfoService.insert(ptMoneyInfo)){
				comPtUserService.updatepartremovemoney(ptMoneyInfo);
                return "true";
            };
        }
            return "出现异常";

    }

	@Override
	public ResultPage<ComCarCharge> selectnull(VoPage voPage, HttpSession session) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
		paramMap.put("limit", voPage.getLimit());
		List<ComCarCharge> list = baseMapper.selectnull(paramMap );
		Integer count = baseMapper.countMyPage(paramMap );
		ResultPage<ComCarCharge>resultPage=new ResultPage<>();
		resultPage.setCode(0);
		resultPage.setCount(count);
		resultPage.setMsg("成功");
		resultPage.setData(list);
		return resultPage;
	}

	//分配车
	@Override
	public Boolean allotcar(Long relacationId, Long id) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("relacationId",relacationId);
		paramMap.put("id",id);
		Integer allotcar = baseMapper.allotcar(paramMap );
		if (allotcar==null){
			return true;
		}
		return false;
	}
	//收车
	@Override
	public Boolean recoverycar( Long id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id",id);
		Integer recoverycar = baseMapper.recoverycar(map );
		if (recoverycar==null){
			return true;
		}
		return false;
	}

    @Override
    public List<ComCarCharge> isCar(Long id, String carPlant) {
        return baseMapper.isCar(id,carPlant);
    }

    @Override
    public List<ComCarCharge> MyCar( Long userId) {
        return baseMapper.MyCar(userId);
    }
}
